# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: inkyu

from recordclass import recordclass
from coin.exchange.base.order_fill_util import FillManager
from coin.exchange.base.order_gateway import Order
from coin.exchange.upbit_v1.kr_rest.product import UpbitProduct

UpbitOrderInternal = recordclass('UpbitOrderInternal',
                                 [
                                     'fill',
                                     'exchange_order_id',
                                     'state',
                                     'timestamp',
                                     'cancel_sent_time',
                                     'cancel_sent_count',
                                     'last_seen_time',
                                 ])


def create_upbit_order_from_snapshot(*, order_snapshot, order_id, tag=None, timestamp=None):
  fill = FillManager(price_orig=order_snapshot.price_orig, qty_orig=order_snapshot.qty_orig)
  order_internal = UpbitOrderInternal(
      fill=fill,
      exchange_order_id=order_snapshot.exchange_order_id,
      state=order_snapshot.state,
      timestamp=order_snapshot.timestamp_created,
      cancel_sent_time=None,
      cancel_sent_count=0,
      last_seen_time=timestamp,
  )
  return Order(
      order_id=order_id,
      product=UpbitProduct.FromStrNativeProduct(order_snapshot.native_symbol),
      price=order_snapshot.price_orig,
      qty=order_snapshot.qty_orig,
      order_side=order_snapshot.order_side,
      order_type=order_snapshot.order_type,
      tag=tag,
      accepted=True,
      cancel_sent=False,  # Consider 'cancel'
      internal=order_internal)


def create_upbit_order(*, order_id, product, price, qty, order_side, order_type, tag):
  fill = FillManager(price_orig=price, qty_orig=qty)
  order_internal = UpbitOrderInternal(
      fill=fill,
      exchange_order_id=None,
      state=None,
      timestamp=None,
      cancel_sent_time=None,
      cancel_sent_count=0,
      last_seen_time=None,
  )
  return Order(order_id=order_id,
               product=product,
               price=price,
               qty=qty,
               order_side=order_side,
               order_type=order_type,
               tag=tag,
               accepted=False,
               cancel_sent=False,
               internal=order_internal)
